package com.zmj.utils;

import com.zmj.dto.UserDTO;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class BlackInterceptor implements HandlerInterceptor {
    static Logger logger = LogManager.getLogger(BlackInterceptor.class);

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
      /* String userName= request.getParameter("userName");
       if (userName.equals("jack")){
           return false;
       }*/
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
      Map<String,Object> model= modelAndView.getModel();
      UserDTO userDTO=(UserDTO) model.get("user");
      if (userDTO.getUserName().equals("jack")){
          modelAndView.setViewName("register");
          modelAndView.addObject("message","此用户是黑名单");
      }else if (userDTO.getUserName().equals("rose")){
         logger.warn("rose已经注册过了");
      }
      logger.info(userDTO.toString());
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
